package m.jclecai.com.utils;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;

/**
 * Created by Dinosa on 2018/1/12.
 * <p>
 * 用来封装摇动的逻辑；3
 */

public class ShakeUtils {

    public boolean isShaking = false;

    private SensorManager sensorManager;
    private ShakeSensorListener shakeListener;

    public Activity mActivity;
    private Vibrator mVibrator;


    public ShakeUtils(Activity activity) {
        mActivity = activity;

        sensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
        shakeListener = new ShakeSensorListener();

        mVibrator = (Vibrator) activity
                .getSystemService(Service.VIBRATOR_SERVICE);
    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    isShaking = false;
                    break;
            }
        }
    };


    private class ShakeSensorListener implements SensorEventListener {
        private static final int ACCELERATE_VALUE = 20;

        @Override
        public void onSensorChanged(SensorEvent event) {

//          Log.e("zhengyi.wzy", "type is :" + event.sensor.getType());

            // 判断是否处于刷新状态(例如微信中的查找附近人)

            if (isShaking) {
                return;
            }

            float[] values = event.values;

            /**
             * 一般在这三个方向的重力加速度达到20就达到了摇晃手机的状态 x : x轴方向的重力加速度，向右为正 y :
             * y轴方向的重力加速度，向前为正 z : z轴方向的重力加速度，向上为正
             */
            float x = Math.abs(values[0]);
            float y = Math.abs(values[1]);
            float z = Math.abs(values[2]);

            Log.e("zhengyi.wzy", "x is :" + x + " y is :" + y + " z is :" + z);

            if (x >= ACCELERATE_VALUE || y >= ACCELERATE_VALUE
                    || z >= ACCELERATE_VALUE) {

                vibrate(500);
                isShaking = true;
                if (mOnShakeUtilsListener != null) {
                    mOnShakeUtilsListener.onShakedListener();
                }
                mHandler.sendEmptyMessageDelayed(0, 2000);
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // TODO Auto-generated method stub
        }
    }


    public void register() {

        //注册监听事件；
        sensorManager.registerListener(shakeListener,
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_FASTEST);

    }

    public void unRegister() {
        // acitivity后台时取消监听
        sensorManager.unregisterListener(shakeListener);
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (mVibrator != null) {
            mVibrator.cancel();
            mVibrator = null;
        }
    }


    public void vibrate(long milliseconds) {

        mVibrator = (Vibrator) mActivity
                .getSystemService(Service.VIBRATOR_SERVICE);
        if (mVibrator != null) {
            if (mVibrator.hasVibrator()) {
                mVibrator.vibrate(milliseconds);
            } else {
                if (mOnShakeUtilsListener != null) {
                    mOnShakeUtilsListener.hasNotVibratorListener();
                }
            }
        }
    }

    public OnShakeUtilsListener mOnShakeUtilsListener;

    public void setOnShakeUtilsListener(OnShakeUtilsListener onShakeUtilsListener) {
        mOnShakeUtilsListener = onShakeUtilsListener;
    }

    /**
     * 这里ShakeUtils的一个回掉操作；
     */
    public interface OnShakeUtilsListener {

        /**
         * 正在摇动的一个逻辑
         */
        public void onShakedListener();

        /**
         * 没有震动器的一个回掉；
         */
        public void hasNotVibratorListener();
    }
}
